package com.wjw.controller.test;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * springboot属性注入：属性值配置在application.yml中
 */
@RestController
public class InjectionController {

    @Value("${name}")
    private String name;

    @Value("${age}")
    private Integer age;

    @Value("${money}")
    private Double money;

    @Value("${flag}")
    private Boolean flag;

    @Value("${birthday}")
    private Date birthday;

    @Value("${strArray}")
    private String [] strArray;

    @Value("${strList}")
    private List<String> strList;

    //注意：注入maps使用#{${属性值}}
    @Value("#{${maps}}")
    private Map<String,String> maps;

    @GetMapping("/inject")
    public String inject(){
        System.out.println("name:===>"+name);
        System.out.println("age:===>"+age);
        System.out.println("money:===>"+money);
        System.out.println("flag:===>"+flag);
        System.out.println("birthday:===>"+birthday);
        System.out.println("遍历数组===========>");
        for (String s : strArray) {
            System.out.println("s:"+s);
        }
        System.out.println("遍历集合===========>");
        strList.forEach((str)-> System.out.println("str:"+str));
        System.out.println("遍历Map===========>");
        maps.forEach((k,v)-> System.out.println("Key:"+k+",Value:"+v));
        return "inject ok";
    }
}
